本次範例以猜數字遊戲為主題,玩法其實就跟終極密碼一樣,猜的數字只要沒有答對都會縮小數字範圍,比較不同的是猜數字的方式,是以支付ether的金額來代表猜什麼數字,所以猜中的人可以拿到contract裡面所有參與者投入的錢
pragma solidity ^0.4.25;
//以finney為單位, 1 finney == 0.001 ether,猜的數字要等同於支付的價格,也就是說猜80就得支付80finney == 0.08 ether
contract GuessNumber {
uint256 answer;
uint256 public LeftRange;
uint256 public RightRange;
event Winner(address indexed _winner, uint256 _answer);
enum State{
Start,
End
}
State state;
modifier Stage(State _state){
require(state == _state);
_;
}
constructor() public {
generate();
}
function generate() private{
answer = uint256(sha256(abi.encodePacked(block.timestamp))) % 100;
}
function guess() public Stage(State.Start) payable{
uint respond = msg.value/10**15;
require(respond <= 100,"不能超過100");
if(respond < answer && respond >= LeftRange){
LeftRange = respond;
}
if(respond > answer){
RightRange = respond;
}
if(respond == answer){
winner(respond);
}
}
function winner(uint256 _respond) private Stage(State.Start){
msg.sender.transfer(address(this).balance);
emit Winner(msg.sender,_respond);
state = State.End;
}
}